// pages/carmanagebox/carlist/carlist.js
import {
  getCarList
} from '../../../utils/networkRequest.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carList: null,
    page: 1,
    last_page: -1, //最后一页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  onShow: function() {
    let self = this;
    getCarList(this.data.page, (r) => {
      console.log(r);
      self.setData({
        carList: r.data,
        last_page: r.pager.last_page
      })
    })
  },
  //前往添加车辆
  goAddCar: function() {
    wx.navigateTo({
      url: "../addcar/addcar"
    })
  },
  //跳转到搜索
  goSearch: function() {
    wx.navigateTo({
      url: '../carsearch/carsearch'
    })
  },
  //编辑车辆
  editCar: function(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: "../addcar/addcar?isEdit=true&item=" + JSON.stringify(item)
    })

  },
  //编辑车辆
  onJieChe: function(e) {
    let item = e.currentTarget.dataset.item
    debugger;
    wx.navigateTo({
      url: "../../jiechebox/main/main?qc=" + item.qcode
    })
  },
  //下啦刷新
  onPullDownRefresh: function() {
    let self = this; 
    getCarList(1, (r) => {
      self.setData({
        carList: r.data,
        page: 1,
        last_page: r.pager.last_page
      })
      wx.stopPullDownRefresh()
    }) 
  },
  //上拉加载
  onReachBottom: function() {
    var self = this;
    let {
      page,
      last_page
    } = this.data;

    if (last_page <= page) {
      wx.showToast({
        icon: "none",
        title: '没有更多数据了.'
      })
    } else {
      page = page + 1;
      getCarList(page, (r) => {
        console.log(r)
        self.setData({
          carList: self.data.carList.concat(r.data),
          page: page,
          last_page: r.pager.last_page
        })
      })
    }

  }

})